<?php
namespace Home\Controller;

use Think\Controller;

/**
 * 订单管理
 * @author DONGGUIYU
 *
 */
class OrderController extends Controller{
    /**
     * 订单列表
     */
    public function orderList(){
        $orderModel = D('Order');
        // 更新所有订单状态
        $rlt = $orderModel->select();
        foreach($rlt as $temp){
            // 判断订单是否过期(如果订单处于未付款状态，那么当出行日期小于等于当前日期时，订单失效
            if($temp['pay_status'] == 1 && $temp['travel_date'] <= date('Y-m-d')){
                unset($where);
                $where['id'] = $temp['id'];
                $data['pay_status'] = 4;// 订单失效
                $data['update_time'] = date('Y-m-d H:i:s');
                $rlt = $orderModel->where($where)->save($data);
            }
        }
        
    	// 订单号
        $orderId = I('post.orderNum', "", 'htmlspecialchars');
        unset($where);
        if(intval($orderId) > 0){
            $where['id'] = array('like','%'.$orderId.'%');
            $this->assign("orderNum", $orderId);
        }
        // 支付状态
        $payStatus = I('post.payStatus', "", 'htmlspecialchars');
        if(intval($payStatus) > 0){
            $where['pay_status'] = $payStatus;
            $this->assign("payStatus", $payStatus);
        }
        // 分页
        $page = setPage($orderModel, $where);
        // 进行分页数据查询
        $orderby['id']='asc';
        // 入口城市
        if(isset($where)){
            $rlt = $orderModel->where($where)->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
        } else {
            $rlt = $orderModel->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
        }
        $orderList = array();
        foreach($rlt as $order){
            $item = $order;
            $item['total_price'] = $order['total_price_adult'] + $order['total_price_child'];
            if($order['product_type'] == 1){
                // 景点
                $attractionModel = D('Attraction');
                unset($where);
                $where['id'] = $order['product_id'];
                $product_name = $attractionModel->where($where)->getField('name');
                $item['product_name'] = $product_name;
            } else if($order['product_type'] == 2){
                // 周边游
                $atroundModel = D('Around');
                unset($where);
                $where['id'] = $order['product_id'];
                $product_name = $atroundModel->where($where)->getField('name');
                $item['product_name'] = $product_name;
            }
            unset($where);
            $where['id'] = $order['user_id'];
            $userModel = D('User');
            $user = $userModel->where($where)->select();
            $item['phone'] = $user[0]['phone'];
            if(!empty($user[0]['real_name'])){
                $item['customer_name'] = $user[0]['real_name'];
            } else if(!empty($user[0]['username']) && $user[0]['username'] != $user[0]['phone']){
                $item['customer_name'] = $user[0]['username'];
            } else{
                $item['customer_name'] = $user[0]['user_code'];
            }
            $orderList[] = $item;
        }
        $show = $page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("orderList", $orderList);
        $this->display('order');
    }
    
    /**
     * 订单详情
     */
    public function orderDetail(){
        $this->assign('title','订单明细');
        // 订单ID
        $id = I('get.id', "", 'htmlspecialchars');
        // 订单model
        $orderModel = D('Order');
        $where['id'] = $id;
        $rlt = $orderModel->where($where)->select();
        if(!empty($rlt) && count($rlt) > 0){
            $order = $rlt[0];
            $order['total_price'] = $order['total_price_adult'] + $order['total_price_child'];
            if($order['product_type'] == 1){
                // 景点
                $attractionModel = D('Attraction');
                unset($where);
                $where['id'] = $order['product_id'];
                $rlt = $attractionModel->where($where)->select();
                $order['product_name'] = $rlt[0]['name'];
                $order['price'] = $rlt[0]['price'];
            } else if($order['product_type'] == 2){
                // 周边游
                $atroundModel = D('Around');
                unset($where);
                $where['id'] = $order['product_id'];
                $rlt = $atroundModel->where($where)->select();
                $order['product_name'] = $rlt[0]['name'];
                $order['price'] = $rlt[0]['price'];
            }
            unset($where);
            $where['id'] = $order['user_id'];
            $userModel = D('User');
            $user = $userModel->where($where)->select();
            $item['phone'] = $user[0]['phone'];
            if(!empty($user[0]['real_name'])){
                $order['customer_name'] = $user[0]['real_name'];
            } else if(!empty($user[0]['username']) && $user[0]['username'] != $user[0]['phone']){
                $order['customer_name'] = $user[0]['username'];
            } else{
                $order['customer_name'] = $user[0]['user_code'];
            }
            $this->assign("order", $order);
        }
        $this->display('orderdetail');
    }
    
    /**
     * 编辑订单
     */
    public function editOrder(){
        $this->assign('title','修改订单');
        // 订单ID
        $id = I('get.id', "", 'htmlspecialchars');
        // 订单model
        $orderModel = D('Order');
        $where['id'] = $id;
        $rlt = $orderModel->where($where)->select();
        if(isset($rlt)){
            $this-> assign('Order', $rlt[0]);
        }
        // 城市
        $cityModel = D('City');
        unset($where);
        $where['parent_id'] = array('gt', 0);
        $cityList = $cityModel->where($where)->select();
        $this->assign("cityList", $cityList);
        $this->display('editOrder');
    }
    
    /**
     * 保存订单
     */
    public function saveOrder(){
        // 订单ID
        $id =  I('post.OrderId', "", 'htmlspecialchars');
        // 城市ID
        $cityId = I('post.city', "", 'htmlspecialchars');
    
        // 订单model
        $orderModel = D('Order');
        $data['city_id'] = $cityId;
        $orderModel->startTrans();
        if(empty($id)){
            // 添加订单
            $rlt = $orderModel->data($data)->add();
        } else {
            // 修改订单
            $where['id'] = $id;
            $rlt = $orderModel->where($where)->save($data);
        }
        if($rlt != 0 && $rlt == false){
            $orderModel->rollback();
            $this->error("保存失败！");
            exit;
        } else {
            $orderModel->commit();
            $this->success("保存成功！", "orderList");
            exit;
        }
    }
    
    /**
     * 删除订单
     */
    public function deleteOrder(){
        $idList = I('post.idList', "", 'htmlspecialchars');
        // 订单model
        $orderModel = D('Order');
        $orderModel->startTrans();
        if(!empty($idList)){
            $idArray = split(',', $idList);
            // 开启事务
            $orderModel->startTrans();
            foreach ($idArray as $id){
                $where['id'] = $id;
                $rlt = $orderModel->where($where)->delete();
                if($rlt == false){
                    // 删除失败时回滚事务
                    $orderModel->rollback();
                    $this->error("删除失败！");
                    exit;
                }
            }
            // 提交事务
            $orderModel->commit();
            $this->success("删除成功！", 'orderList');
        }
    }
}

?>